<?php
/**
 * Created by PhpStorm.
 * User: 跳梁
 * Date: 2017/1/18
 * Time: 11:11
 */
class RechargeCardAction extends Action{
    //充值卡列表
    public function getCardList($name=null,$start=0,$limit=10){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user["customerId"];
        Session::switchCustomer(0);
        $rechargeCardModel = M("module_rechargecard");
        Session::switchCustomer($customerId);
        $where['cid']=array("eq",$customerId);
        $where['deleted']=array("eq",0);
        $name?$where['name']=array("like","%$name%"):"";
        $arr = $rechargeCardModel->where($where)->limit($start,$limit)->order("updateTime desc")
            ->field("id,name,receiveType,money,status,createTime,copiesNum,surplusNum,copiesNum*money as totalMoney,surplusNum*money as surplusMoney,deleted")
            ->select();
        if($arr){
            $count = $rechargeCardModel->where($where)->count();
            Output::instance()->objArray($arr,$count);
        }else{
            Output::instance()->resultCode();
        }

    }
    //充值卡详情
    public function getCardInfo($id){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user["customerId"];
        Session::switchCustomer(0);
        $rechargeCardModel = M("module_rechargecard");
        Session::switchCustomer($customerId);
        $cardInfo = $rechargeCardModel->alias("a")
            ->join("wx_".$customerId."_material b on a.newsId=b.id")
            ->field("a.*,b.xml")
            ->where("a.id=$id")->find();
        if($cardInfo['qrcodeLocation']){
            $qrcodeLocation = explode("|",$cardInfo['qrcodeLocation']);
            $cardInfo['codeLocationX'] = $qrcodeLocation[0];
            $cardInfo['codeLocationY'] = $qrcodeLocation[1];
        }
        if($cardInfo['qrcodeSize']){
            $qrcodeSize = explode("*",$cardInfo['qrcodeSize']);
            $cardInfo['codeSizeX'] = $qrcodeSize[0];
            $cardInfo['codeSizeY'] = $qrcodeSize[1];
        }
        Output::instance()->object($cardInfo);
    }
    //保存充值卡设置
    public function saveRechargeCard($name,$money,$copiesNum,$status,$isEffective,$startTime,$endTime,$receiveType,
                                     $qrcodeBack=null,$qrcodeLocation=null,$qrcodeSize=null,$newsId=null,$codeType=null,$id=null){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user["customerId"];
        Session::switchCustomer(0);
        $rechargeCardModel = M("module_rechargecard");
        Session::switchCustomer($customerId);
        $time = date("Y-m-d H:i:s");
        $data['name']=$name;
        $data['money']=$money;
        $data['copiesNum']=$copiesNum;
        $data['status']=$status;
        $data['isEffective']=$isEffective;
        $data['startTime']=$startTime;
        $data['endTime']=$endTime;
        $data['receiveType']=$receiveType;
        $data['updateTime']=$time;
        isset($qrcodeBack)?$data['qrcodeBack']=$qrcodeBack:"";
        isset($qrcodeLocation)?$data['qrcodeLocation']=$qrcodeLocation:"";
        isset($qrcodeSize)?$data['qrcodeSize']=$qrcodeSize:"";
        isset($newsId)?$data['newsId']=$newsId:"";
        isset($codeType)?$data['codeType']=$codeType:"";
        if($id){
            $cardInfo = $rechargeCardModel->where("id=$id")->find();
            if($cardInfo['copiesNum']>$copiesNum){
                Output::instance()->resultCode(1,"总份数不能少于剩余份数！");
                return;
            }else{
                $data['surplusNum'] = $copiesNum-$cardInfo['copiesNum']+$cardInfo['surplusNum'];
            }
            $result = $rechargeCardModel->where("id=$id")->save($data);
        }else{
            $data['cid']=$customerId;
            $data['surplusNum']=$copiesNum;
            $data['createTime']=$time;
            $result = $rechargeCardModel->add($data);
        }
        if($result){
            Output::instance()->resultCode();
        }else{
            Output::instance()->resultCode(code_sql_error);
        }
    }
    //修改充值卡状态（删除或停用）
    public function setRechargeCard($id,$status,$deleted){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user["customerId"];
        Session::switchCustomer(0);
        $rechargeCardModel = M("module_rechargecard");
        Session::switchCustomer($customerId);
        $data['deleted']=$deleted;
        $data['status']=$status;
        $data['updateTime']=date("Y-m-d H:i:s");
        $result = $rechargeCardModel->where("id=$id")->save($data);
        if($result){
            Output::instance()->resultCode();
        }else{
            Output::instance()->resultCode(code_sql_error);
        }
    }
    //充值卡充值统计
    public function rechargeRecord($id,$start=0,$limit=10){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user["customerId"];
        Session::switchCustomer(0);
        $rechargeRecordModel = M("module_recharge_record");
        Session::switchCustomer($customerId);
        $where['a.cid']=array("eq",$customerId);
        $where['a.rechargecardId']=array("eq",$id);
        $arr = $rechargeRecordModel->alias("a")
            ->join("wx_".$customerId."_user_weixin_info b on a.userId=b.userId")
            ->field("b.nickname,b.headimgurl,a.id,a.money,a.createTime")
            ->where($where)
            ->limit($start,$limit)
            ->order("a.createTime desc")
            ->select();
        if($arr){
            $count = $rechargeRecordModel->alias("a")->where($where)->count();
            Output::instance()->objArray($arr,$count);
        }else{
            Output::instance()->resultCode();
        }

    }
    //导出历史
    public function exportList($id,$start=0,$limit=10){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user["customerId"];
        Session::switchCustomer(0);
        $expordModel = M("module_rechargecard_expord");
        Session::switchCustomer($customerId);
        $where['a.cid']=array("eq",$customerId);
        $where['a.rechargecardId']=array("eq",$id);
        $arr = $expordModel->alias("a")
            ->join("wx_module_rechargecard b on a.rechargecardId=b.id")
            ->field("a.*,a.num*b.money as totalPrice")
            ->where($where)->limit($start,$limit)->order("a.createTime desc")->select();
        if($arr){
            $count = $expordModel->alias("a")->where($where)->count();
            Output::instance()->objArray($arr,$count);
        }else{
            Output::instance()->resultCode();
        }
    }
    //二维码导出
    public function rechargeCardExpord($id,$num){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user["customerId"];
        Session::switchCustomer(0);
        $rechargeCardModel = M("module_rechargecard");
        $cardExportModel = M("module_rechargecard_expord");
        Session::switchCustomer($customerId);
        $cardInfo = $rechargeCardModel->where("id=$id")->find();
        if($cardInfo['surplusNum']<$num){
            Output::instance()->resultCode(1,"充值卡数量不足");
            return;
        }
        $count = $cardExportModel->where("rechargecardId=$id")->count();
        if($cardInfo['codeType']==0){
            $qrType = QR_LIMIT_SCENE;
        }else{
            $qrType = QR_SCENE;
        }
        $path=LOCAL_PIC_PATH . "rechargecard/";
        for($i=0;$i<$num;$i++){
            $serialNumber = $count.(10000+$i);
            $data = QRCode::add("充值卡" .$cardInfo['name'].$serialNumber, null,"purse", $id, "scanCardcode",$qrType, '', '', $customerId);
            $qrImg = $data["img"];
            R("Coupon/createImage",array($path,$serialNumber,$qrImg,$cardInfo['qrcodeBack'],$cardInfo['qrcodeLocation'],$cardInfo['qrcodeSize']));
        }
        $result = R("Coupon/getIDZip",array($path,"rechargecard_code_".time().".zip"));
        if($result->path){
            $data['cid']=$customerId;
            $data['rechargecardId']=$id;
            $data['num']=$num;
            $data['fileUrl']=$result->path;
            $data['createTime']=date("Y-m-d H:i:s");
            $result = $cardExportModel->add($data);
            Output::instance()->object($data);
        }else{
            Output::instance()->resultCode();
        }
    }
}